package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * xdx
 * 2020/12/21
 * 23:13
 */
@RestController
public class LogoutController {

    /**
     * 删除同名cookie ：access_token
     * 发送的请求是user/login？？
     * @return
     */
    @GetMapping("user/loginOut")
    public ResponseData logout(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        Cookie cookieToDelete = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("access_token")){
                cookieToDelete = cookie;
                cookieToDelete.setValue(null);
                cookieToDelete.setPath("/");
                cookieToDelete.setMaxAge(0);
                response.addCookie(cookieToDelete);
            }
        }


        ResponseData<Object> objectResponseData = new ResponseData<>();

        return new ResponseUtil().setErrorMsg("token已经失效");
    }

}
